package com.yiwei.zxp.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.model.MessageCenterModel;
import com.yiwei.zxp.base.BaseActivity;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.view.BottomIconText;
import com.yiwei.zxp.view.V19FrameLayout;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class BussinessMsgTotalActivity extends BaseActivity {


    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.view_one)
    View viewOne;
    @BindView(R.id.btn_one)
    BottomIconText btnOne;
    @BindView(R.id.txt_one_number)
    TextView txtOneNumber;
    @BindView(R.id.view_two)
    View viewTwo;
    @BindView(R.id.btn_two)
    BottomIconText btnTwo;
    @BindView(R.id.txt_two_number)
    TextView txtTwoNumber;
    @BindView(R.id.view_three)
    View viewThree;
    @BindView(R.id.btn_three)
    BottomIconText btnThree;
    @BindView(R.id.txt_three_number)
    TextView txtThreeNumber;
    @BindView(R.id.view_four)
    View viewFour;
    @BindView(R.id.btn_four)
    BottomIconText btnFour;
    @BindView(R.id.txt_four_number)
    TextView txtFourNumber;
    private Unbinder unbiner;
    private final int TypeOne = 4;
    private final int TypeTwo = 3;
    private final int TypeThree = 2;
    private final int TypeFour = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bussiness_message_total);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();

    }

    @Override
    protected void onResume() {
        super.onResume();
        getMsgTotal();
    }

    private void init() {
        txtTitle.setText("消息");
    }

    public void getMsgTotal() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        loadingDialog.show();

        Api.api_service.getMessageCenter().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        loadingDialog.dismiss();
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    //消息类型id 1:系统消息，2：订单消息，3：物流消息，4：账户消息
                    List<MessageCenterModel.DatasBean.ItemBean> data = result.datas.item;
                    if (data == null || data.size() == 0) {
                        return;
                    }


                    for (int i = 0; i < data.size(); i++) {
                        MessageCenterModel.DatasBean.ItemBean item = data.get(i);
                        int type = item.type;
                        switch (type) {
                            case 1:
                                if (item.count == 0) {
                                    txtFourNumber.setVisibility(View.GONE);
                                } else {
                                    txtFourNumber.setVisibility(View.VISIBLE);
                                    txtFourNumber.setText(item.count + "");
                                }
                                break;
                            case 2:
                                if (item.count == 0) {
                                    txtThreeNumber.setVisibility(View.GONE);
                                } else {
                                    txtThreeNumber.setVisibility(View.VISIBLE);
                                    txtThreeNumber.setText(item.count + "");
                                }
                                break;
                            case 3:
                                if (item.count == 0) {
                                    txtTwoNumber.setVisibility(View.GONE);
                                } else {
                                    txtTwoNumber.setVisibility(View.VISIBLE);
                                    txtTwoNumber.setText(item.count + "");
                                }
                                break;
                            case 4:
                                if (item.count == 0) {
                                    txtOneNumber.setVisibility(View.GONE);
                                } else {
                                    txtOneNumber.setVisibility(View.VISIBLE);
                                    txtOneNumber.setText(item.count + "");
                                }
                                break;
                        }
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }

    @OnClick({R.id.btn_back, R.id.btn_one, R.id.btn_two, R.id.btn_three, R.id.btn_four})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_one:
                Intent intent = new Intent(this, BussinessMsgActivity.class);
                intent.putExtra("type", TypeOne);
                startActivity(intent);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                break;
            case R.id.btn_two:
                Intent intent1 = new Intent(this, BussinessMsgActivity.class);
                intent1.putExtra("type", TypeTwo);
                startActivity(intent1);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                break;
            case R.id.btn_three:
                Intent intent2 = new Intent(this, BussinessMsgActivity.class);
                intent2.putExtra("type", TypeThree);
                startActivity(intent2);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                break;
            case R.id.btn_four:
                Intent intent3 = new Intent(this, BussinessMsgActivity.class);
                intent3.putExtra("type", TypeFour);
                startActivity(intent3);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                break;
        }
    }
}
